<template>
	<view class="main" v-if="dataLoaded && !isInvalid">
		<view class="main-tips">
			<view class="main-tips_icon">
			</view>
			<view class="main-tips_text">
				请认真核对您要提货拿走的商品信息
			</view>
		</view>

		<view class="main-list">
			<view class="main-item" v-for="item in pickupInfo.scanGoodsList" :key="item.id">
				<view class="main-item_left">
					<image class="main-item_img" v-if="item.goodsImg" :src="item.goodsImg" mode=""></image>
				</view>
				<view class="main-item_right">
					<view class="main-item_name">
						{{item.goodsName}}
					</view>

					<view style="display: flex;align-items: center;justify-content: space-between;margin-top: 28rpx;">
						<view class="main-item_unit">
							单位：{{item.goodsUnit}}
						</view>
						<view class="main-item_number">
							x{{item.goodsNumber}}
						</view>
					</view>

				</view>
			</view>
		</view>
		<view style="height: 150rpx;">
		</view>

		<view class="main-button">
			<view class="main-button_btn" @click="confirm">
				我已确认将商品提走
			</view>
		</view>
	</view>

	<view class="main-invalid" v-else-if="dataLoaded && isInvalid">
		<image class="main-invalid_icon" :src="invalidImg" mode="aspectFit"></image>
		<view class="main-invalid_text">
			{{invalidText}}
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pickupInfo: {},
				orderNo: '',
				dataLoaded: false,
				isInvalid: true,
				invalidCode: ''
			};
		},
		onLoad(res) {
			if (res.scene) {
				const scene = decodeURIComponent(res.scene)
				this.orderNo = scene
			}
		},
		computed: {
			invalidImg(){
				if(['1101','1102','1104'].includes(this.invalidCode)) {
					return 'https://wechat-pictures-1301970825.file.myqcloud.com/pickup/icon_codeInvalid@3x.png'
				}
				if(['1103'].includes(this.invalidCode)) {
					return 'https://wechat-pictures-1301970825.file.myqcloud.com/pickup/icon_weizhuce@3x.png'
				}
				
				return ''
			},
			invalidText() {
				if(['1101','1102','1104'].includes(this.invalidCode)) {
					return '该二维码已失效'
				}
				if(['1103'].includes(this.invalidCode)) {
					return '暂不支持非本门店顾客扫码'
				}
				
				return ''
			}
		},
		onShow() {
			console.log('onShow');
			//获取用户token
			this.login()
			// this.getInfo()

		},
		methods: {
			login(isFromRegister) {
				// 获取微信code
				var _this = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						_this.check_login(loginRes.code);
					},
					fail: (err) => {
						console.error('wx.login fail, reason:', err)
					}
				})
			},
			// 检测登录
			async check_login(code) {
				var _this = this;
				var data = {
					code: code,
					requestCode: ''
				};
				try {
					const res = await this.$NoteApi.doRequest("get", "login", data);
					if (res.data.code == 0) {
						// 已注册
						uni.setStorageSync("token", res.data.data.token)
						uni.setStorageSync("userInfo", res.data.data);

						if (res.data.data.isNew == 1) {
							// 新用户
							uni.redirectTo({
								url: '/pages/newAnswer/register?type=index'
							})
							return false
						} else {
							this.getInfo();
						}
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
					}

				} catch (error) {}
			},
			getInfo() {
				this.$javaApiErp.doRequest("post", "/rkkt/scan/getScanInfo", {
					orderNo: this.orderNo
				}, 'json').then(res => {
					this.dataLoaded = true
					if (res.data.code == 0) {

						if (res.data.data.code) {
							const code = res.data.data.code;
							this.isInvalid = true
							this.invalidCode = code
						} else {
							this.isInvalid = false
							this.pickupInfo = res.data.data
						}
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none',
							duration: 99999,
							mask: true
						})
					}
				})
			},
			confirm() {
				uni.showModal({
					title: "提示",
					content: "确认已经收取到了商品吗？",
					cancelText: '没收到',
					cancelColor: '#959AAF',
					confirmText: '已收到',
					confirmColor: '#5CAE77',
					success: e => {
						if (e.confirm) {
							this.$javaApiErp.doRequest("post", "/rkkt/scan/confirmPickup", {
								orderNo: this.orderNo
							}, 'json').then(res => {
								if (res.data.code == 0) {
									uni.showToast({
										title: "确认完成",
										icon: 'success',
										duration: 3000,
										mask: true
									})
									
									setTimeout(()=> {
										uni.switchTab({
											url: '/pages/sindex/index'
										})
									}, 2500)
								} else {
									uni.showToast({
										title: res.data.message,
										icon: 'none',
										duration: 3000,
										mask: true
									})
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		position: relative;
		width: 100vw;
		min-height: 100vh;
		background: #FFFFFF;
		padding: 50rpx 30rpx;
		box-sizing: border-box;
		padding-bottom: env(safe-area-inset-bottom);

		&-tips {
			width: 100;
			display: flex;
			align-items: center;

			&_icon {
				width: 6rpx;
				height: 28rpx;
				background: #18AA59;
				border-radius: 3rpx;
			}

			&_text {
				font-weight: 600;
				font-size: 42rpx;
				color: #18AA59;
				margin-left: 4rpx;
			}
		}

		&-list {
			margin-top: 4rpx;
			width: 100%;
		}

		&-item {
			width: 100%;
			border-bottom: 2rpx solid #F1F1F1;
			padding: 40rpx 0;
			display: flex;
			justify-content: space-between;


			&_left {
				width: 160rpx;
				height: 160rpx;
				// background: #D8D8D8;
			}

			&_img {
				width: 160rpx;
				height: 160rpx;
				object-fit: fill;
			}

			&_right {
				width: 514rpx;
			}

			&_name {
				font-weight: 400;
				font-size: 42rpx;
				color: #333333;
				line-height: 60rpx;
				height: 120rpx;
				word-break: break-all;
				overflow: hidden;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
			}

			&_unit {
				font-weight: 400;
				font-size: 40rpx;
				color: #666666;
			}

			&_number {
				font-weight: 400;
				font-size: 44rpx;
				color: #333333;
			}
		}

		&-button {
			position: fixed;
			left: 0;
			bottom: 0;
			padding-bottom: env(safe-area-inset-bottom);
			width: 100%;
			height: 132rpx;
			background: #FFFFFF;
			display: flex;
			align-items: center;
			justify-content: center;
			box-sizing: content-box;

			&_btn {
				width: 690rpx;
				height: 98rpx;
				background: #18AA59;
				font-size: 44rpx;
				border-radius: 12rpx;
				color: #FFFFFF;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
		
		&-invalid {
			width: 100vw;
			height: 100vh;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-direction: column;
			
			&_icon {
				width: 400upx;
				height: 400upx;
				object-fit: contain;
				margin-top: -200upx;
			}
			
			&_text {
				width: 384upx;
				font-weight: 400;
				font-size: 46upx;
				color: #333333;
				line-height: 65upx;
				text-align: center;
			}
		}
	}
</style>